
import com.luoc.base.config.ShiroApplication;
import com.luoc.base.config.mybatis.mapper.SysUserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Author: luoc
 * @Date: 2019/7/24 16:37
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShiroApplication.class)
public class testHelloWorld {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Test
    public void testHelloWorld() {
        // SecurityManager Factory
        Factory<SecurityManager> factory =
                new IniSecurityManagerFactory("classpath:shiro.ini");
        // SecurityManager
        org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
        // 获取SecurityManager
        SecurityUtils.setSecurityManager(securityManager);

        // Subject
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");

        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            // 爬出异常
            System.out.println("error");
        }
        //断言用户已经登录
        Assert.assertEquals(true, subject.isAuthenticated());
        System.out.println(subject.isAuthenticated());

        subject.logout();
    }

    @Test
    public void testMapper() {
        System.out.println(sysUserMapper.findByName("admin1"));
    }
}
